package com.yutian.selfservice.support.api;

import com.yutian.selfservice.support.mapper.*;
import com.yutian.selfservice.support.utils.SpringUtil;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 办事指南明细信息
 */
@Data
public class ServiceGuideDetails {
    private Long serverId;

    private BasicInformation basicInformation;
    private List<ApprovalResult> approvalResult;
    private GeneralInformation generalInformation;
    private List<ApplicationMaterial> applicationMaterial;
    private List<AcceptCondition> acceptCondition;
    private List<HandlerProcess> handlerProcess;
    private List<Foundation> foundation;
    private List<IntermediaryService> intermediaryService;
    private List<CommonQuestion> commonQuestion;

    public ServiceGuideDetails() {

    }

    public ServiceGuideDetails(Long serverId) {
        this.serverId = serverId;
    }

    public ServiceGuideDetails details() {
        this.basicInformation = getMapper(BasicInformationMapper.class).selectOne(new BasicInformation().setServerId(this.serverId));
        if (Objects.isNull(this.basicInformation)) {
            return this;
        }
        this.approvalResult = getMapper(ApprovalResultMapper.class).select(new ApprovalResult().setServerId(this.serverId));
        this.generalInformation = getMapper(GeneralInformationMapper.class).selectOne(new GeneralInformation().setServerId(this.serverId));
        this.applicationMaterial = getMapper(ApplicationMaterialMapper.class).select(new ApplicationMaterial().setServerId(this.serverId));
        this.acceptCondition = getMapper(AcceptConditionMapper.class).select(new AcceptCondition().setServerId(this.serverId));
        this.handlerProcess = getMapper(HandlerProcessMapper.class).select(new HandlerProcess().setEventCode(this.basicInformation.getEventCode()));
        this.foundation = getMapper(FoundationMapper.class).select(new Foundation().setServerId(this.serverId));
        this.intermediaryService = getMapper(IntermediaryServiceMapper.class).select(new IntermediaryService().setServerId(this.serverId));
        this.commonQuestion = getMapper(CommonQuestionMapper.class).select(new CommonQuestion().setServerId(this.serverId));
        setApplicationMaterialDown();
        setLegal();
        return this;
    }

    /**
     * 法律依据设置
     */
    private void setLegal() {
        if (CollectionUtils.isEmpty(this.applicationMaterial) || CollectionUtils.isEmpty(this.foundation)) {
            return;
        }
        Map<String, List<String>> map = this.foundation.stream().collect(Collectors.groupingBy(Foundation::getLawType,
                Collectors.mapping(x -> x.getLawName() + ": " + x.getClause(), Collectors.toList())));
        //4	申请材料依据
        this.applicationMaterial.forEach(x -> x.setMaterialLegalGround(map.get("4")));
    }

    /**
     * 下载文件设置
     */
    private void setApplicationMaterialDown() {
        if (CollectionUtils.isEmpty(applicationMaterial) && CollectionUtils.isEmpty(this.approvalResult)) {
            return;
        }
        List<Long> ids = this.applicationMaterial.stream().map(x -> x.getMaterialId()).collect(Collectors.toList());
        ids.addAll(this.approvalResult.stream().map(x -> x.getMaterialReferenceId()).collect(Collectors.toList()));
        DownloadMaterialMapper downloadMaterialMapper = getMapper(DownloadMaterialMapper.class);
        List<ApplicationMaterial.DownloadMaterial> downloadMaterials =
                downloadMaterialMapper.selectByMaterialIds(ids);
        Map<Long, List<ApplicationMaterial.DownloadMaterial>> group = downloadMaterials.stream().collect(Collectors.groupingBy(ApplicationMaterial.DownloadMaterial::getMaterialId));
        this.applicationMaterial.forEach(x -> x.setDownloadMaterials(group.getOrDefault(x.getMaterialId(), new ArrayList<>())));
        this.approvalResult.forEach(x -> x.setDownloadMaterials(group.getOrDefault(x.getMaterialReferenceId(), new ArrayList<>())));
    }


    private <T> T getMapper(Class<T> clz) {
        return SpringUtil.getBean(clz);
    }

}
